package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product" +
                "(name,normal_price,price,content,is_show,is_recommend,img_href,cuisine_id) " +
                "values ('" + productModel.getProductName() + "'," +
                "" + productModel.getNormalPrice() + "," +
                "" + productModel.getPrice() + "," +
                "'" + productModel.getContent() + "'," +
                "" + productModel.getIsShow() + "," +
                "" + productModel.getIsRecommend() + "," +
                "'" + productModel.getImgHref() + "'," +
                "" + productModel.getCuisineId() + ")";

        return JDBC.excuteUpdate(sql);
    }

    @Override
    public boolean findByName(String name) {
        String sql = "select * from product where name='" + name + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        boolean isHave = false;
        try {
            while (resultSet.next()) {
                isHave = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isHave;
    }

    @Override
    public List<ProductModel> findAll() {
        String sql = "select p.*,c.name as cuisine_name from product p,cuisine c where p.cuisine_id=c.id";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setProductName(resultSet.getString("name"));
                productModel.setNormalPrice(resultSet.getFloat("normal_price"));
                productModel.setPrice((float) resultSet.getDouble("price"));
                productModel.setContent(resultSet.getString("content"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                productModel.setIsRecommend(resultSet.getInt("is_recommend"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setCuisineId(resultSet.getInt("cuisine_id"));
                productModel.setCuisineName(resultSet.getString("cuisine_name"));
                list.add(productModel);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int del(int id) {
        String sql = "delete from product where id=" + id;
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public ProductModel findById(int id) {
        String sql = "select * from product where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        ProductModel productModel = new ProductModel();
        try {
            while (resultSet.next()) {
                productModel.setId(resultSet.getInt("id"));
                productModel.setProductName(resultSet.getString("name"));
                productModel.setNormalPrice(resultSet.getFloat("normal_price"));
                productModel.setPrice((float) resultSet.getDouble("price"));
                productModel.setContent(resultSet.getString("content"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                productModel.setIsRecommend(resultSet.getInt("is_recommend"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setCuisineId(resultSet.getInt("cuisine_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productModel;
    }

    @Override
    public int update(ProductModel productModel) {
        StringBuilder sql = new StringBuilder("update product set ");
        sql.append("name='").append(productModel.getProductName()).append("',");
        sql.append("normal_price=").append(productModel.getNormalPrice()).append(",");
        sql.append("price=").append(productModel.getPrice()).append(",");
        sql.append("content='").append(productModel.getContent()).append("',");
        sql.append("is_show=").append(productModel.getIsShow()).append(",");
        sql.append("is_recommend=").append(productModel.getIsRecommend()).append(",");
        if (!"undefined".equals(productModel.getImgHref())) {
            sql.append("img_href='").append(productModel.getImgHref()).append("',");
        }
        sql.append("cuisine_id=").append(productModel.getCuisineId()).append(" ");
        sql.append("where id=").append(productModel.getId());

        return JDBC.excuteUpdate(sql.toString());
    }
}
